#include "historywindow.h"
#include <QHeaderView>
#include <QMessageBox>

HistoryWindow::HistoryWindow(const QString &username, Database *db, QWidget *parent)
    : QDialog(parent)
    , username(username)
    , db(db)
{
    setupUI();
    createConnections();
    setupTableModel();
}

HistoryWindow::~HistoryWindow()
{
}

void HistoryWindow::setupUI()
{
    setWindowTitle("借阅历史");
    setMinimumSize(800, 600);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 标题
    QLabel *titleLabel = new QLabel("借阅历史记录");
    titleLabel->setStyleSheet("font-size: 24px; font-weight: bold; margin: 20px;");
    titleLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(titleLabel);

    // 历史记录表格
    historyTableView = new QTableView(this);
    historyTableView->setStyleSheet(
        "QTableView {"
        "   border: 1px solid #ddd;"
        "   border-radius: 4px;"
        "   background-color: white;"
        "}"
        "QTableView::item {"
        "   padding: 5px;"
        "}"
        "QHeaderView::section {"
        "   background-color: #f5f5f5;"
        "   padding: 5px;"
        "   border: 1px solid #ddd;"
        "   font-weight: bold;"
        "}"
    );
    mainLayout->addWidget(historyTableView);

    // 按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();

    closeButton = new QPushButton("关闭", this);
    closeButton->setStyleSheet(
        "QPushButton {"
        "   background-color: #dc3545;"
        "   color: white;"
        "   border: none;"
        "   padding: 8px 16px;"
        "   border-radius: 4px;"
        "   min-width: 100px;"
        "}"
        "QPushButton:hover {"
        "   background-color: #c82333;"
        "}"
    );
    buttonLayout->addWidget(closeButton);

    mainLayout->addLayout(buttonLayout);
}

void HistoryWindow::createConnections()
{
    connect(closeButton, &QPushButton::clicked, this, &HistoryWindow::onCloseClicked);
}

void HistoryWindow::setupTableModel()
{
    historyModel = new QSqlTableModel(this, db->getDatabase());
    historyModel->setTable("borrow_records");
    historyModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    // 设置表头
    historyModel->setHeaderData(0, Qt::Horizontal, "借阅ID");
    historyModel->setHeaderData(1, Qt::Horizontal, "用户名");
    historyModel->setHeaderData(2, Qt::Horizontal, "图书ID");
    historyModel->setHeaderData(3, Qt::Horizontal, "借阅时间");
    historyModel->setHeaderData(4, Qt::Horizontal, "应还时间");
    historyModel->setHeaderData(5, Qt::Horizontal, "实际归还时间");
    historyModel->setHeaderData(6, Qt::Horizontal, "状态");

    // 设置过滤器，只显示当前用户的历史记录
    historyModel->setFilter(QString("username = '%1'").arg(username));
    historyModel->select();

    historyTableView->setModel(historyModel);
    historyTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    historyTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    historyTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    historyTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void HistoryWindow::onCloseClicked()
{
    reject();
} 